package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Food;
import model.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dao.UserDao;

@Controller
public class HomepageComtroller {
	String url = "";
	UserDao dao = new UserDao();
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView register() {
		Map<String, Object> model = new HashMap<String, Object>();
		url = "register";
		model.put("url", url);
		return new ModelAndView("../../Homepage", model);
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		Map<String, Object> model = new HashMap<String, Object>();
		User user = new User();
		url = "login";
		model.put("url", url);
		model.put("user", user);
		return new ModelAndView("../../Homepage", model);
	}

	@RequestMapping(value = "/loginexcute", method = RequestMethod.POST)
	public ModelAndView loginExcute(@ModelAttribute User user) {
		User temp = dao.getUser(user.getName());
		String name = temp.getName();
		String pass = temp.getPassword();
		
		Map<String, Object> model = new HashMap<String, Object>();
		if (name==null) {
			String mes = " user khong ton tai ";
			model.put("mes", mes);
			url = "login";
			model.put("url", url);
			
			
		} else if  (pass.equals(user.getPassword())) {
			if ("User".equals(temp.getRole())) {
				url = "foodlist";
				model.put("user", temp);
				model.put("url", url);
				
			} else if ("Admin".equals(temp.getRole())){
				url = "userlist";
				model.put("user", temp);
				model.put("url", url);
				
			}
		}

		// xu ly dang nhap
		
		
		
		return new ModelAndView("../../Homepage", model);
	}
}
